package com.qf.service.impl;

import com.alibaba.fastjson.JSON;
import com.qf.common.R;
import com.qf.mapper.CourseMapper;
import com.qf.mapper.impl.CourseMapperImpl;
import com.qf.pojo.Course;
import com.qf.service.CourseService;
import com.qf.utils.DBUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class CourseServiceImpl implements CourseService {

    private CourseMapper courseMapper = new CourseMapperImpl();

    /**
     * 是否可以添加课程
     *
     * @param request
     * @return
     */
    @Override
    public boolean isAdd(HttpServletRequest request) {
        String name = request.getParameter("name");
        Course course = null;
        try {
            course = DBUtils.commonQueryObj(Course.class, "select * from course where name = ?", name);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        if (course == null) {
            return true;
        }
        return false;
    }

    /**
     * 添加课程
     *
     * @param request
     * @param response
     * @return
     */
    @Override
    public boolean add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        if (isAdd(request)) {
            courseMapper.add(name);
            response.getWriter().write(JSON.toJSONString(R.success("添加成功", null)));
            return true;
        }
        request.setAttribute("msg", "课程已存在");
        response.getWriter().write(JSON.toJSONString(R.fail("课程已存在")));
        return false;
    }

    /**
     * 初始化课程信息
     *
     * @param request
     * @param response
     */
    @Override
    public void initModify(HttpServletRequest request, HttpServletResponse response) {
        String courseId = request.getParameter("courseId");
        Course course = courseMapper.getCourse(Integer.parseInt(courseId));
        if (course == null) {
            request.setAttribute("msg", "课程不存在");
            return;
        }
        request.setAttribute("course", course);
    }

    /**
     * 根据课程ID修改课程信息
     *
     * @param request
     * @param response
     */
    @Override
    public boolean modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String courseId = request.getParameter("courseId");
        String name = request.getParameter("name");
        if (isAdd(request)) {
            courseMapper.update(Integer.parseInt(courseId), name);
            response.getWriter().write(JSON.toJSONString(R.success("修改成功", null)));
            return true;
        } else request.setAttribute("msg", "课程已存在");
        response.getWriter().write(JSON.toJSONString(R.fail("课程已存在")));
        return false;
    }

    /**
     * 获取课程列表
     *
     * @param request
     * @param response
     */
    @Override
    public void getCourses(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Course> courses = courseMapper.getCourses();
        response.getWriter().write(JSON.toJSONString(R.success("获取课程列表成功！", courses)));
    }

    /**
     * 根据课程ID删除课程
     *
     * @param request
     * @param response
     */
    @Override
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (request.getParameter("courseId") == null) {
            response.getWriter().write(JSON.toJSONString(R.success("删除失败，courseId不能为空", null)));
        }
        String courseId = request.getParameter("courseId");
        courseMapper.delete(Integer.parseInt(courseId));
        response.getWriter().write(JSON.toJSONString(R.success("删除成功", null)));
    }

    @Override
    public void getCourse(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String courseId = request.getParameter("courseId");
        Course course = courseMapper.getCourse(Integer.parseInt(courseId));
        if (course == null) {
            response.getWriter().write(JSON.toJSONString(R.fail("课程不存在")));
            return;
        }
        response.getWriter().write(JSON.toJSONString(R.success("获取课程成功", course)));
    }
}
